package com.zhongge.servlet;

import com.zhongge.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName UserLoginServlet
 * @Description TODO
 * @Author zhongge
 * @Version 1.0
 */
@WebServlet("/login")
public class UserLoginServlet extends HttpServlet {
    /**
     * 登录一般是：post请求
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        //获取前端请求带来的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //检查密码是否正确
        boolean result = new UserService().login(username, password);
        if (result) {
            //转发到学生列表
            request.getRequestDispatcher("/list").forward(request, response);
        } else {
           //跳转到登录界面==》并且将错误信息放到request
            request.setAttribute("tip_info", "用户名或者密码错误！！！");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
    }
}

